package com.czx.drivingtest.controller;

import com.czx.drivingtest.model.ResponseData;
import com.czx.drivingtest.model.dto.Question;
import com.czx.drivingtest.service.QuestionService;
import com.czx.drivingtest.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author czx
 * @version 2020/3/12 8:54 下午
 * @since JDK12
 */
@RestController
@RequestMapping(value = "/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 根据id获取问题
     * @param questionId
     * @param subject 科目
     * @return
     */
    @GetMapping(value = "/{questionId}")
    public ResponseData<Question> getQuestionById(@PathVariable int questionId, Integer subject) {
        Question question;
        //如果前端传来的id时-1，就说明这个用户还没开始答题，我们就返回第一道题
        if (questionId == -1) {
            question = questionService.getFirstQuestion(subject);
        } else {
            question = questionService.getQuestionById(questionId);
        }
        return new ResponseData<>(0, "success", question);
    }

    @PostMapping
    public ResponseData<Void> addQuestion(@RequestBody Question question) {
        questionService.addQuestion(question);
        return new ResponseData<>(0, "success");
    }

    /**
     * 获取下一道题
     * @return
     */
    @GetMapping("/next/{questionId}")
    public ResponseData<Question> getNextQuestion(@PathVariable int questionId, Integer subject) {
        Question nextQuestion = questionService.getNextQuestion(questionId, subject);
        return new ResponseData<>(0, "success", nextQuestion);
    }

    /**
     * 获取前一道题
     * @return
     */
    @GetMapping("/pre/{questionId}")
    public ResponseData<Question> getPreQuestion(@PathVariable int questionId, Integer subject) {
        Question preQuestion = questionService.getPreQuestion(questionId, subject);
        return new ResponseData<>(0, "success", preQuestion);
    }

    /**
     * 获取3道题目
     * @param middleId 中间那道题目的id
     * @param subject 科目
     * @return
     */
    @GetMapping("/list3")
    public ResponseData<List<Question>> get3QuestionList(Integer middleId, Integer subject) {
        List<Question> questionList = questionService.get3Questions(middleId, subject);
        return new ResponseData<>(0, "success", questionList);
    }

    /**
     * 获取题目列表
     * @param questionIds 题目id,多个用逗号分隔
     * @return
     */
    @GetMapping("/list/{questionIds}")
    public ResponseData<List<Question>> getQuestionList(@PathVariable String questionIds) {
        List<Integer> idList = ConvertUtil.strSplit2IntList(questionIds, ",");
        List<Question> questionList = questionService.getByIds(idList);
        return new ResponseData<>(0, "success", questionList);
    }

    /**
     * 随机获取题目
     * @param total 要获取的题目数量
     * @return
     */
    @GetMapping("/random")
    public ResponseData<List<Question>> getRandomQuestionList(Integer total, Integer subject) {
        int maxTotal = 100;
        int minTotal = 0;
        if (total > maxTotal || total < minTotal) {
            return new ResponseData<>(-1, "随机题目数量不合法");
        }
        List<Question> questionList = questionService.getRandomQuestionList(total, subject);
        return new ResponseData<>(0, "success", questionList);
    }




}
